var statisticsProjectVue = new Vue({
	el: "body",
	data: {
		fileData: [],
		allGuanCang: [],
		indexNum: 0,
		statrYear: "",
		startWeek: "",
		endYear: "",
		endWeek: "",
		nowPage: 0, //保存当前加载的页数
		loadMore: false, //控制显示加载更多信息
		totalNum: 0, //当前信息总量
		noMore: false, //控制显示已无更多数据信息
		loading: false //控制在下拉一次未完成之间不再执行下拉
	},
	methods: {
		jieShou: function() {
			$(".statistics-file-tabbar>div").eq(1).removeClass("forbidden")
			$(".statistics-file-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			_this.indexNum = 2;
			_this.totalNum = 0;
			searchGuanCangFile(_this, "接收")
			searchAllGuanCangFile(_this, "接收")
			searchFileNumChart(_this)
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-file-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		chaXun: function() {
			$(".statistics-file-tabbar>div").eq(1).removeClass("forbidden")
			$(".statistics-file-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			_this.indexNum = 1
			searchSearch(_this)
			searchSearchNumChart(_this)
			console.log(_this.fileData)
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-file-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		ruKu: function() {
			$(".statistics-file-tabbar>div").eq(1).removeClass("forbidden")
			$(".statistics-file-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			_this.indexNum = 3
			_this.totalNum = 0
			searchGuanCangFile(_this, "入库")
			searchAllGuanCangFile(_this, "入库")
			searchFileNumChart(_this)
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-file-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})

		},
		guancang: function() {
			$(".statistics-file-tabbar>div").eq(1).addClass("forbidden")
			$(".statistics-file-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			_this.indexNum = 4
			_this.totalNum = 0
			searchGuanCangFile(_this, "馆藏")
			searchAllGuanCangFile(_this, "馆藏")
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-file-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		back: function() {
			window.history.go(-1); //返回上一页
		},
		firstClick: function(a, b) {
			var thisClassName = $(".firstTitle").eq(b).siblings("ul").attr("class")
			if(thisClassName) {
				var ret = new RegExp("show").test(thisClassName)
				console.log(ret)
				if(ret) {
					$(".firstTitle").eq(b).siblings("ul").removeClass("show")
					$(".secondTitle").siblings("ul").removeClass("show")
				} else {
					$(".firstTitle").siblings("ul").removeClass("show")
					$(".firstTitle").eq(b).siblings("ul").addClass("show")
				}
			} else {
				$(".firstTitle").siblings("ul").removeClass("show")
				$(".secondTitle").siblings("ul").removeClass("show")
			}
		},
		secondClick: function(a, b) {
			var thisClassName = $(".secondTitle").eq(b).siblings("ul").attr("class")
			if(thisClassName) {
				var ret = new RegExp("show").test(thisClassName)
				console.log(ret)
				if(ret) {
					$(".secondTitle").eq(b).siblings("ul").removeClass("show")
				} else {
					$(".secondTitle").siblings("ul").removeClass("show")
					$(".secondTitle").eq(b).siblings("ul").addClass("show")
				}
			} else {
				$(".secondTitle").siblings("ul").removeClass("show")
			}
		}
	},
	created: function() {
		_this = this //防止出现闭包

		_this.startYear = getUrlItem("startYear")
		_this.startWeek = getUrlItem("startWeek")
		_this.endYear = getUrlItem("endYear")
		_this.endWeek = getUrlItem("endWeek")

		//判断是否需要下拉刷新
		$(document.body).infinite().on("infinite", function() {
			//一页显示10条数据,因此当列表中数据大于等于10时才可以进行下拉刷新
			var nowSelect = $(".statistics-file-tabbar .active").html(); //判断当前是否在数据图页面，如果在数据图页面，下来刷新不起作用
			if(_this.fileData.length >= 12 && _this.totalNum > 0 && nowSelect == "数据详情") {
				//防止多次进行刷新重复操作
				if(_this.loading) return;
				_this.loading = true;
				_this.loadMore = true;
				var beClick = $(".statistics-file-title .tab .active").html(); //判断当前点击的是下方是下方三个tab的哪一个
				console.log(beClick)
				setTimeout(function() {
					var canPage = Math.ceil(_this.totalNum / 12)
					//	当前页大于分页总页则显示已无更多数据
					switch(beClick) {
						case "查询":
							if(canPage > _this.nowPage) {
								searchMoreSearch(_this)
								_this.loadMore = false;
								_this.loading = false;
							} else {
								_this.noMore = true;
								_this.loadMore = false;
								_this.loading = true;
							}
							break;
					};
				}, 2000);
			} else {
				_this.noMore = false;
			}
		});

	},
	ready: function() {
		$(function() {
			FastClick.attach(document.body);
		});

		$(".firstTitle").click(function() {
			var thisClassName = $(this).siblings("ul").attr("class")
			if(thisClassName) {
				var ret = new RegExp("show").test(thisClassName)
				console.log(ret)
				if(ret) {
					$(this).siblings("ul").removeClass("show")
					$(".secondTitle").siblings("ul").removeClass("show")
				} else {
					$(".firstTitle").siblings("ul").removeClass("show")
					$(this).siblings("ul").addClass("show")
				}
			} else {
				$(".firstTitle").siblings("ul").removeClass("show")
				$(".secondTitle").siblings("ul").removeClass("show")
			}
		})

		$(".secondTitle").click(function() {
			var thisClassName = $(this).siblings("ul").attr("class")
			if(thisClassName) {
				var ret = new RegExp("show").test(thisClassName)
				console.log(ret)
				if(ret) {
					$(this).siblings("ul").removeClass("show")
				} else {
					$(".secondTitle").siblings("ul").removeClass("show")
					$(this).siblings("ul").addClass("show")
				}
			} else {
				$(".secondTitle").siblings("ul").removeClass("show")
			}
		})

		$(".statistics-file-title .tab>div").each(function(i) {
			$(".statistics-file-title .tab>div").eq(i).click(function() {
				$(".statistics-file-title .tab div").removeClass("active")
				$(this).addClass("active")
			})
		})
		//顶部tab切换

		$(".statistics-file-tabbar>div").eq(1).click(function() {
			var beclick = $(".statistics-file-title  .active").html()
			if(beclick != "馆藏") {
				$(".statistics-file-tabbar>div").removeClass("active")
				$(this).addClass("active")
				$(".statistics-file-body").hide()
				$(".statistics-file-title").addClass("hidden")
				$(".statistics-file-cart").removeClass("hidden")
			} else {

			}
		})

		$(".statistics-file-tabbar>div").eq(0).click(function() {
			$(".statistics-file-tabbar>div").removeClass("active")
			$(this).addClass("active")
			$(".statistics-file-body").show()
			$(".statistics-file-title").removeClass("hidden")
			$(".statistics-file-cart").addClass("hidden")
		})
		//底部tab切换

		$(function() {
			var chartHeight = $(".statistics-file-cart").height() + 50 //tab 图表高度+留白高度
			var windowHeight = $(window).height() //设备高度
			if(chartHeight > windowHeight) {
				$(".statistics-file-cart").css("margin-bottom", "45px") //图表背景超过设备高度无需充满全屏		
			} else {
				setFullScreen(".statistics-file-cart", 50) //图表背景充满全屏
			}
			var type = getUrlItem("type");
			if(type == "chaxun") {
				$(".statistics-file-title .tab>div").eq(0).click()
			}
			if(type == "jieshou") {
				$(".statistics-file-title .tab>div").eq(1).click()
			}
			if(type == "ruku") {
				$(".statistics-file-title .tab>div").eq(2).click()
			}
			if(type == "guancang") {
				$(".statistics-file-title .tab>div").eq(3).click()
			}
			$(".statistics-file-tabbar>div").eq(0).click()
		})

	}
})

function searchSearch(a) {
	a.fileData = [] //切换tab前先清空列表数据防止残留数据触发加载更多操作
	a.nowPage = 1;
	a.noMore = false;
	a.loading = false;
	a.loadMore = false;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getArcList?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"pagesize": 12,
			"pageno": a.nowPage
		},
		dataType: "json",
		success: function(jsondata) {
			a.totalNum = jsondata.total_count
			a.fileData = jsondata.data
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function searchMoreSearch(a) {
	a.nowPage++
		$.ajax({
			type: "post",
			url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getArcList?callback=?",
			data: {
				"year": a.startYear,
				"week": a.startWeek,
				"year2": a.endYear,
				"week2": a.endWeek,
				"pagesize": 12,
				"pageno": a.nowPage
			},
			dataType: "json",
			success: function(jsondata) {
				for(var i = 0; i < jsondata.data.length; i++) {
					a.fileData.push(jsondata.data[i]);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {}
		});

}

function getUrlItem(name) { //id
	var url = location.search
	start = url.indexOf(name + "=")
	if(start != -1) {
		start = start + name.length + 1
		end = url.indexOf("&", start)
		if(end == -1) {
			end = url.length
			return unescape(url.substring(start, end))
		}
		return unescape(url.substring(start, end))
	}
	return '';
}

function searchSearchNumChart(a) {
	var x = []; //X轴
	var y = []; //Y轴
	var xtext = []; //X轴TEXT
	var color = ["#FCB954", "#25AA1A", "#F8863A", "#04A9F5"];
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getArcSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			console.log(jsondata)
			var jsonList = []
			var s = 1;
			for(var item in jsondata) {
				var key = {
					TYPE: item,
					NUM: jsondata[item]
				}
				jsonList.push(key)
			}
			for(var key in jsonList) {
				jsonList[key].y = jsonList[key].NUM; //给Y轴赋值
				xtext[key] = jsonList[key].TYPE; //给X轴TEXT赋值
				jsonList[key].color = color[key];
			}
			console.log(jsonList)
			chart.series[0].setData(jsonList); //数据填充到highcharts上面
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
	var chart = new Highcharts.Chart({
		chart: {
			renderTo: 'searchChart',
			type: 'column' //显示类型 柱形
		},
		title: {
			text: '' //图表的标题
		},
		//当鼠标经过时的提示设置
		tooltip: {
			headerFormat: '<span style="font-size:11px"><b>类别：<b></span><br>',
			pointFormat: '<span style="color:{point.color}">{point.TYPE}</span><br><b>{point.y:.1f} 个</b><br/>'
		},
		xAxis: {
			categories: xtext
		},
		yAxis: {
			title: {
				text: '', //Y轴的名称
				style: {
					color: '#333333',
					fontWeight: 'bold',
					fontSize: '14px'
				}
			}
		},
		series: [{
			name: "所处环节"
		}],
		plotOptions: { //plotOptions全局设置
			series: { //对series的全局设置                        
				cursor: 'pointer',
				dataLabels: {
					enabled: true, //是否在节点上显示数据
					color: '#333',
					align: 'center',
					style: { fontSize: '13px' }
				}
			}
		},
		credits: {
			enabled: false //隐藏右下角网址
		},
		exporting: {
			enabled: false //隐藏右上角打印
		},
		legend: {
			enabled: false //去掉底部的标题
		}
	});
}

function setFullScreen(Name, Height) {
	$(document).ready(function() { //DOM加载后就执行
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).resize(function() { //改变窗体大小时     *resize()变化或者改变事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).scroll(function() { //浏览器有滚动条时的操作   scroll()滚动事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
}

//馆藏数据接口
function searchGuanCangFile(a, b) {
	a.fileData = [] //切换tab前先清空列表数据防止残留数据触发加载更多操作
	a.nowPage = 1;
	a.noMore = false;
	a.loading = false;
	a.loadMore = false;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getArcTreeSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"curcode": b
		},
		dataType: "json",
		success: function(jsondata) {
			a.fileData = jsondata.Children
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchAllGuanCangFile(a, b) {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getArcTreeSum?callback=?",
		data: {
			"year": 0,
			"week": 0,
			"year2": 0,
			"week2": 0,
			"curcode": b
		},
		dataType: "json",
		success: function(jsondata) {
			a.allGuanCang = jsondata.Children
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchFileNumChart(a) {
	var x = []; //X轴
	var y = []; //Y轴
	var xtext = []; //X轴TEXT
	var color = ["#FCB954", "#25AA1A", "#F8863A", "#04A9F5"];
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getArcTotalSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			var dataJson = {
				"接收": jsondata.jieshouNow,
				"入库": jsondata.rukuNow
			}
			var jsonList = []
			var s = 1;
			for(var item in dataJson) {
				var key = {
					TYPE: item,
					NUM: dataJson[item]
				}
				jsonList.push(key)
			}
			for(var key in jsonList) {
				jsonList[key].y = jsonList[key].NUM; //给Y轴赋值
				xtext[key] = jsonList[key].TYPE; //给X轴TEXT赋值
				jsonList[key].color = color[key];
			}
			console.log(jsonList)
			chart.series[0].setData(jsonList); //数据填充到highcharts上面
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
	var chart = new Highcharts.Chart({
		chart: {
			renderTo: 'searchChart',
			type: 'column' //显示类型 柱形
		},
		title: {
			text: '' //图表的标题
		},
		//当鼠标经过时的提示设置
		tooltip: {
			headerFormat: '<span style="font-size:11px"><b>类别：<b></span><br>',
			pointFormat: '<span style="color:{point.color}">{point.TYPE}</span><br><b>{point.y:.1f} 个</b><br/>'
		},
		xAxis: {
			categories: xtext
		},
		yAxis: {
			title: {
				text: '', //Y轴的名称
				style: {
					color: '#333333',
					fontWeight: 'bold',
					fontSize: '14px'
				}
			}
		},
		series: [{
			name: "所处环节"
		}],
		plotOptions: { //plotOptions全局设置
			series: { //对series的全局设置                        
				cursor: 'pointer',
				dataLabels: {
					enabled: true, //是否在节点上显示数据
					color: '#333',
					align: 'center',
					style: { fontSize: '13px' }
				}
			}
		},
		credits: {
			enabled: false //隐藏右下角网址
		},
		exporting: {
			enabled: false //隐藏右上角打印
		},
		legend: {
			enabled: false //去掉底部的标题
		}
	});
}